﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Dotnet.Code;
using Dotnet.Code.Net;

namespace 三方平台接口示例
{
    public partial class Form1 : Form
    {
        private const string URL = "http://58.49.169.228:33403/"; //http://127.0.0.1:33401/""; // "http://58.49.169.228:33403/"; http://10.11.2.103:33401/


        public Form1()
        {
            InitializeComponent();
        }
        private string GetSigns(string method,DataBase obj)
        {
            obj.method = method;
            obj.timestamp = DateTime.Now.ToUnixTimeString();
            string tmp = "test_appid" + "test_dwbm" + obj.method + obj.timestamp + "v1.0";
            string sign = Md5.md5(tmp, 32);
            return sign;
        }
        private void HttpPost<T>(string apiUrl, T data,Action<string> action)
        {
            HttpHelper http = new Dotnet.Code.Net.HttpHelper();
            HttpItem item = new Dotnet.Code.Net.HttpItem();
            item.URL = $"{URL}api/{apiUrl}";
            item.Method = "POST";
            item.ContentType = "application/json;charset=utf-8";
            item.Postdata = data.ToJson();
            item.PostDataType = PostDataType.String;
            Stopwatch sw = Stopwatch.StartNew();
            Task.Factory.StartNew(() =>
            {
                try
                {
                    HttpResult result = http.GetHtml(item);
                    string info = $"【请求URL】{item.URL} 耗时:{sw.Elapsed.TotalSeconds.ToString("N2")}秒 {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}\r\n" +
                        $"【方法】{(data as DataBase).method}\r\n【参数】\r\n{item.Postdata}\r\n" +
                        $"【返回结果】{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}\r\n{result.Html}\r\n========================================";
                    if (action != null) action(info);
                    Console.WriteLine(info);
                }
                catch (Exception ex)
                {
                    string info = $"【请求URL】{item.URL} 耗时:{sw.Elapsed.TotalSeconds.ToString("N2")}秒 {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}\r\n" +
                        $"【方法】{(data as DataBase).method}\r\n【参数】\r\n{item.Postdata}\r\n" +
                        $"【返回结果】{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}\r\n{ex.Message}\r\n{ex.StackTrace}========================================";
                    if (action != null) action(info);
                    Console.WriteLine(info);
                }
            });
        }

        private void btnDwjr_Click(object sender, EventArgs e)
        {
            OrgInfo org = new OrgInfo();
            org.sign = GetSigns("add",org);
            org.dwmc = txtDwmc.Text==""? "测试单位":txtDwmc.Text;
            org.dwdz = txtDwdz.Text == "" ? "湖北武汉" : txtDwdz.Text; 
            org.dwzbX = 110.23;
            org.dwzbY = 220.36;
            org.dwsxlb = "3333";
            org.xfzrrxm = "4444";
            org.xfglrxm = "99999";
            org.xfglrdh = "5555";
            org.wbdwmc = "66666";
            this.Enabled = false;
            HttpPost("Org/orgInfo", org, (str) => {
                this.Enabled = true;
            });
        }
        private void btnorgInfoUpdate_Click(object sender, EventArgs e)
        {
            OrgInfo org = new OrgInfo();
            org.sign = GetSigns("update", org);
            org.dwmc = txtDwmc.Text == "" ? "测试单位" : txtDwmc.Text;
            org.dwdz = txtDwdz.Text == "" ? "湖北武汉" : txtDwdz.Text;
            org.dwzbX = 110.23;
            org.dwzbY = 220.36;
            org.dwsxlb = "3333";
            org.xfzrrxm = "4444";
            org.xfglrxm = "99999";
            org.xfglrdh = "5555";
            org.wbdwmc = "66666";
            this.Enabled = false;
            HttpPost("Org/orgInfo", org,(str)=> {
                this.Enabled = true;
            });
        }

        private void btnBuildings_Click(object sender, EventArgs e)
        {
            Buildings buildings = new Buildings();
            buildings.sign = GetSigns("add",buildings);

            buildings.jzmc = txt_jzmc.Text == "" ? "testbuilding" : txt_jzmc.Text;
            buildings.jzzbX = 123.00;
            buildings.jzzbY = 456.23;
            buildings.jzgd = 25.69;
            buildings.jzmj = 1250.69;
            //buildings.jzpmt = "";
            buildings.jzpmt = "xxxx";
            buildings.jzlc = 10;
            buildings.wbuildinginfoid= txtWbuildinginfoid.Text == "" ? "testbuilding" : txtWbuildinginfoid.Text;
            this.Enabled = false;
            HttpPost("OrgAssets/Buildings", buildings, (str) => {
                this.Enabled = true;
            });
        }
        private void btnofficearea_Click(object sender, EventArgs e)
        {
            officearea area = new officearea();
            area.sign = GetSigns("add",area);
            area.mc = txtMC.Text==""?"TTTTT":txtMC.Text;
            area.qmc = txtQmc.Text==""? "yyy-XX-XXXX": txtQmc.Text; 
            area.wofficeareaid = txtWofficeareaid.Text==""?"444444":txtWofficeareaid.Text;
            area.wdeviceid = "66666";
            this.Enabled = false;
            HttpPost("officearea/save", area, (str) => {
                this.Enabled = true;
            });
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnFireContrlRoom_Click(object sender, EventArgs e)
        {
            FireContrlRoom fireContrlRoom = new FireContrlRoom();
            fireContrlRoom.sign=GetSigns("add",fireContrlRoom);
            fireContrlRoom.xksmc = txt_xksmc.Text == "" ? "TTTTT" : txt_xksmc.Text;
            fireContrlRoom.xksdh = txt_xksdh.Text == "" ? "TTTTT" : txt_xksdh.Text;
            fireContrlRoom.xkswz = txt_xkswz.Text == "" ? "TTTTT" : txt_xkswz.Text;
            fireContrlRoom.xfkzszbrxm = txt_xfkzszbrxm.Text == "" ? "TTTTT" : txt_xfkzszbrxm.Text;
            this.Enabled = false;
            HttpPost("OrgAssets/FireContrlRoom",fireContrlRoom,(str) => {
                this.Enabled = true;
            });
        }

        private void btnFireAlarmController_Click(object sender, EventArgs e)
        {
            FireAlarmController fireContrlRoom = new FireAlarmController();
            fireContrlRoom.sign = GetSigns("add", fireContrlRoom);
            fireContrlRoom.wlbm = txt_wlbm.Text == "" ? "TTTTT" : txt_wlbm.Text;
            fireContrlRoom.zjpp = txt_zjpp.Text == "" ? "TTTTT" : txt_zjpp.Text;
            fireContrlRoom.zjxh = txt_zjxh.Text == "" ? "TTTTT" : txt_zjxh.Text;
            fireContrlRoom.zjwz = txt_zjwz.Text == "" ? "TTTTT" : txt_zjwz.Text;
            fireContrlRoom.sblx = txt_sblx.Text == "" ? "TTTTT" : txt_sblx.Text;
            fireContrlRoom.wdeviceid= txtWdeviceid.Text == "" ? "11111" : txtWdeviceid.Text;
            this.Enabled = false;
            HttpPost("OrgAssets/FireAlarmController", fireContrlRoom, (str) => {
                this.Enabled = true;
            });
        }


    }
}
